package jp.co.ikd.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	/** 年月(区切り文字なし) */
	public static final String PLANE_YYYYMM = "yyyyMM";

	/** 年月日(区切り文字なし) */
	public static final String PLANE_YYYYMMDD = "yyyyMMdd";

	/** 年月日時分秒(区切り文字なし) */
	public static final String YYYYMMDDHH24MISS = "yyyyMMddHHmmss";

	/** 年月日時分秒(区切り文字なし) */
	public static final String HH24MISS = "HHmmss";

	/** 年月(区切り文字あり) */
	public static final String FORMATED_YYYYMM = "yyyy/MM";

	/** 年月日(区切り文字あり) */
	public static final String FORMATED_YYYYMMDD = "yyyy/MM/dd";

	/** 年月日時分秒(区切り文字あり) */
	public static final String FORMATED_YYYYMMDDHH24MISS = "yyyy/MM/dd HH:mm:ss";

	/** 年月日時分秒(区切り文字あり) */
	public static final String FORMATED_HH24MISS = "HH:mm:ss";

	private DateUtil() {
	};

	public static Date getDate(int year, int month, int day, int hour, int minute, int second) {
		Calendar cal = Calendar.getInstance();
		cal.setLenient(false);
		cal.set(year, month - 1, day, hour, minute, second);
		return cal.getTime();
	}

	public static Date addDate(Date date, int year, int month, int day, int hour, int minute, int second) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, year);
		cal.add(Calendar.MONTH, month);
		cal.add(Calendar.DATE, day);
		cal.add(Calendar.HOUR_OF_DAY, hour);
		cal.add(Calendar.MINUTE, minute);
		cal.add(Calendar.SECOND, second);

		return cal.getTime();
	}

	public static Date addYear(Date date, int year) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, year);

		return cal.getTime();
	}

	public static String addYear(String date, String pattern, int year) throws ParseException {

		Date d = toDate(date, pattern);
		d = addYear(d, year);
		return format(d, pattern);
	}

	public static Date addMonth(Date date, int month) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, month);

		return cal.getTime();
	}

	public static Date addDay(Date date, int day) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, day);

		return cal.getTime();
	}

	public static Date subtractYear(Date date, int year) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, -year);

		return cal.getTime();
	}

	public static Date subtractMonth(Date date, int month) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, -month);

		return cal.getTime();
	}

	public static Date subtractDay(Date date, int day) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, -day);

		return cal.getTime();
	}

	public static String format(Date date, String format) {

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		sdf.setLenient(false);

		return sdf.format(date);
	}

	public static String formatYMDFormat(Date date) {

		return format(date, DateUtil.FORMATED_YYYYMMDD);
	}

	public static String format(String date, String format) throws ParseException {

		Date d = toDate(date, format);

		return format(d, format);
	}

	public static Date toDateYMDFormat(String date) throws ParseException {

		return toDate(date, DateUtil.FORMATED_YYYYMMDD);
	}

	public static Date toDate(String date, String format) throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		sdf.setLenient(false);

		return sdf.parse(date);
	}

	/**
	 * 日付から西暦の年を取得する
	 * @param date 日付
	 * @return 西暦の年
	 */
	public static int getYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.YEAR);
	}

	/**
	 * 日付から月を取得する
	 * @param date 日付
	 * @return 月(1～12)
	 */
	public static int getMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MONTH) + 1;
	}

	/**
	 * 日付から日を取得する
	 * @param date 日付
	 * @return 日
	 */
	public static int getDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 日時から時間を取得する
	 * @param date 日時
	 * @return 時間(0～23)
	 */
	public static int getHour(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 日時から分を取得する
	 * @param date 日時
	 * @return 分
	 */
	public static int getMinute(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MINUTE);
	}

	/**
	 * 日時から秒を取得する
	 * @param date 日時
	 * @return 秒
	 */
	public static int getSecond(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.SECOND);
	}

	/**
	 * 日時から初日を取得する
	 * @param date 日時
	 * @return 初日
	 */
	public static int getFirstDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.getActualMinimum(Calendar.DATE);
	}

	public static Date getFirstDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		cal.set(Calendar.DAY_OF_MONTH, getFirstDay(date));

		return cal.getTime();
	}

	/**
	 * 日時から末日を取得する
	 * @param date 日時
	 * @return 末日
	 */
	public static int getLastDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.getActualMaximum(Calendar.DATE);
	}
	
	public static Date getLastDate(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		cal.set(Calendar.DAY_OF_MONTH, getLastDay(date));

		return cal.getTime();
	}

	public static String getBussinessYear(Date date) {

		int year = getYear(date);
		int month = getMonth(date);

		if (month < 4) {
			return Integer.toString(year - 1);
		}
		return Integer.toString(year);
	}

	public static String getBussinessYear(String date) throws ParseException {
		return getBussinessYear(toDateYMDFormat(date));
	}
}
